// ========================================================
//  法宝数据（CP_MagicWeapon.js）
// =========================================================

/*:
 * @plugindesc (v1.02)装备等级系统
 * @author 小c
 * @version 1.0.2
 * @date 3/25/2018
 *
 * @param ---Base Params---
 * @desc  
 * @default  
 * 
 * @param Init Level
 * @parent ---Base Params---
 * @desc 法宝初始等级
 * 默认值：1
 * @default 1
 * 
 * @param Init Exp
 * @parent ---Base Params---
 * @desc 法宝初始经验
 * 默认值：0
 * @default 0
 *
 * @param Init Exp Formula
 * @parent ---Base Params---
 * @desc 默认法宝经验公式
 * level - 法宝当前等级  默认值：3*level*level+40*level+50
 * @default 3 * level * level + 40 * level + 50
 *
 * @param Max Level
 * @parent ---Base Params---
 * @desc 法宝等级上限
 * 默认值：15
 * @default 15
 *
 * @param ---Plus Params---
 * @desc  
 * @default  
 *
 * @param Enemy Min Exp
 * @parent ---Plus Params---
 * @desc 敌人携带法宝经验最小值
 * 默认值：1
 * @default 1
 *
 * @param Use Exp
 * @parent ---Plus Params---
 * @desc 主动法宝使用时获得的经验值
 * 默认值：1
 * @default 1
 *
 * @param Exp Rate Not Battler
 * @parent ---Plus Params---
 * @type number
 * @decimals 2
 * @min 0
 * @desc 后队人员获得法宝经验比率
 * @default 0.80
 *
 * @param ---UI Params---
 *
 * @param Use UI Code
 * @parent ---UI Params---
 * @type boolean
 * @on Use
 * @off Don't Use
 * @desc 是否使用此脚本UI
 * 此UI基于YEP物品装备核心制作，不采用请关闭
 * @default true
 *
 * @param Level Font Size
 * @parent ---UI Params---
 * @type number
 * @desc 图标上显示等级的字体大小
 * 默认值：16
 * @default 16
 *
 * @param Level Font Color
 * @parent ---UI Params---
 * @type number
 * @desc 图标上显示等级的字体颜色
 * 默认值：6
 * @default 6
 * 
 * @param Level Gauge Color 1
 * @parent ---UI Params---
 * @type number
 * @desc 等级条颜色1
 * @default 0
 *
 * @param Level Gauge Color 2
 * @parent ---UI Params---
 * @type number
 * @desc 等级条颜色2
 * @default 0
 *
 * @param Exp Gauge Color 1
 * @parent ---UI Params---
 * @type number
 * @desc 经验条颜色1
 * @default 0
 *
 * @param Exp Gauge Color 2
 * @parent ---UI Params---
 * @type number
 * @desc 经验条颜色2
 * @default 0
 * 
 * @help
 * 装备等级插件，即轩辕剑的法宝系统。
 * 此插件需要YEP装备核心等独立装备插件支持
 *
 * 以下标签配置于武器和防具的备注中
 * <Magic Weapon>
 * 表示该装备具有【法宝属性】，有等级和经验等参数，若没有此标签，后面所
 * 提武器防具配置标签全部无效。
 * 注：任何装备均可配置此标签，不仅仅是装备类型所定义的【法宝】。
 * 
 * <Init Level: x>
 * <Init Exp: x>
 * 分别设置法宝刚获得时的初始等级和初始经验为x，若没有配置此标签，则使
 * 用插件参数的设置。
 * 
 * <Max Level: x>
 * 设置法宝等级上限，若没有配置此标签，则采用插件参数的配置。
 *
 *  <Exp Formula: formula>
 * 设置法宝升到下一级所需经验公式（公式中可采用level表示法宝当前等级）
 * 为formula，若没有配置此标签，则采用插件参数的配置。
 * 
 * <Use Exp: x>
 * 设置战斗中的主动法宝每命中一次所得法宝经验，若没有配置此标签，则采用
 * 插件参数的配置。
 * 
 * <Magic Weapon LevelUp Effect>
 * //JavaScript code
 * $gameParty.gainItem($dataItems[5], 1);
 * user.addState(15);
 * this.gainExp(2);
 * </Magic Weapon LevelUp Effect>
 * 装备升级效果，装备升级执行JavaScript脚本
 * 脚本中item为这个装备的data对象，user为装备者
 * this指针表示这个装备的Game_Item对象
 *
 * 以下标签用于敌人的备注中
 * <Magic Weapon Exp: x>
 * 设置击倒敌人后所得法宝经验为x。若没有配置此标签，则采用插件参数的配
 * 置。​
 *
 * 插件指令
 * MagicWeaponLevelUp actorId slotId level
 * 对指定角色的指定装备槽的装备升级level级
 *
 * 预留JavaScript函数
 * MagicWeaponManager.findMagicWeaponBySkill(subject, skill);
 * 寻找指定角色装备槽中包括指定技能的装备，返回Game_Item数组
 * 其中的skill为$dataSkills中的对象，通过action.item()可获得
 *
 * MagicWeaponManager.findMagicWeaponByBaseItemId(subject, baseId, isWeapon)
 * 寻找指定角色的装备槽中指定id的装备对象，返回Game_Item数组
 * 其中baseId为该装备在数据库中的id，isWeapon记录是否为武器
 *
 * Game_Item类扩展函数
 * item.level() 装备等级，装备没有等级返回-1
 * item.maxLevel() 装备最高等级，没有等级返回-1
 * item.exp() 装备当前经验，装备每次升级后经验会归零重算，没有经验返回-1
 * item.nextLevelExp() 装备升级所需经验，没有装备经验返回-1，满级装备不会
 * 返回-1
 * item.gainExp(exp) 装备获得指定经验值
 * item.levelUp() 装备升1级
 *
 * 更新日志
 * v1.00
 * 插件完成
 *
 * v1.01
 * 增加装备升级效果
 *
 * v1.02
 * 修复因丢失升级物品而导致升级时死机的BUG
 */

var Imported = Imported || {};
Imported.CP_MagicWeapon = true;

var MagicWeaponManager = MagicWeaponManager || {};
var params = PluginManager.parameters("CP_MagicWeapon");

MagicWeaponManager.INIT_LEVEL = Math.round(Number(params["Init Level"]) || 1);
MagicWeaponManager.INIT_EXP = Math.round(Number(params["Init Exp"]) || 0);
MagicWeaponManager.EXP_FORMULA = params["Init Exp Formula"]; 
MagicWeaponManager.MAX_LEVEL = Math.round(Number(params["Max Level"]) || 15);
MagicWeaponManager.ENEMY_MIN_EXP = Math.round(Number(params["Enemy Min Exp"]) || 0);
MagicWeaponManager.USE_EXP = Math.round(Number(params["Use Exp"]) || 0);
MagicWeaponManager.EXP_RATE_NOT_BATTLER = Number(params["Exp Rate Not Battler"]) || 0;

MagicWeaponManager.USE_UI_CODE = eval(params["Use UI Code"]);
MagicWeaponManager.LEVEL_FONT_SIZE = Number(params["Level Font Size"]) || 16;
MagicWeaponManager.LEVEL_FONT_COLOR = Number(params["Level Font Color"]) || 0;
MagicWeaponManager.LEVEL_GAUGE_COLOR1 = Number(params["Level Gauge Color 1"]) || 0;
MagicWeaponManager.LEVEL_GAUGE_COLOR2 = Number(params["Level Gauge Color 2"]) || 0;
MagicWeaponManager.EXP_GAUGE_COLOR1 = Number(params["Exp Gauge Color 1"]) || 0;
MagicWeaponManager.EXP_GAUGE_COLOR2 = Number(params["Exp Gauge Color 2"]) || 0;

MagicWeaponManager.selectFlag = false;

MagicWeaponManager.DATABASE_LOADED = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function(){
	var loaded = MagicWeaponManager.DATABASE_LOADED.call(this);
	if(!loaded)
		return loaded;

	MagicWeaponManager.loadMagicWeaponData($dataWeapons);
	MagicWeaponManager.loadMagicWeaponData($dataArmors);
	MagicWeaponManager.loadEnemyMagicWeaponExp();

	return loaded;
};

MagicWeaponManager.loadMagicWeaponData = function(dataArray){
	for(var i = 1; i < dataArray.length; i++){
		var equip = dataArray[i];
		equip.initLevel = -1;
		equip.level = -1;
		equip.exp = 0;
		equip.expFormula = null;
		equip.maxLevel = -1;
		equip.useExp = -1;
		equip.equipLevelUpEffect = null;

		if(this.isMagicWeapon(equip)){
			equip.initLevel = Math.round(Number(equip.meta["Init Level"]) || MagicWeaponManager.INIT_LEVEL);
			equip.level = equip.initLevel;
			equip.exp = Math.round(Number(equip.meta["Init Exp"]) || MagicWeaponManager.INIT_EXP);
			equip.expFormula = equip.meta["Exp Formula"] || null;
			equip.maxLevel = Math.round(Number(equip.meta["Max Level"]) || MagicWeaponManager.MAX_LEVEL);
			equip.useExp = Math.round(Number(equip.meta["Use Exp"]) || MagicWeaponManager.USE_EXP);

			if(equip.meta["Magic Weapon LevelUp Effect"]){
				var reg = /<Magic Weapon LevelUp Effect>([\s\S]*)<\/Magic Weapon LevelUp Effect>/;
				reg.exec(equip.note);

				equip.equipLevelUpEffect = RegExp.$1;
			}
		}
	}
};

MagicWeaponManager.loadEnemyMagicWeaponExp = function(){
	for(var i = 1; i < $dataEnemies.length; i++){
		var enemy = $dataEnemies[i];
		enemy.magicWeaponExp = Math.round(Number(enemy.meta["Magic Weapon Exp"])) || MagicWeaponManager.ENEMY_MIN_EXP;
	}
};

MagicWeaponManager.isMagicWeapon = function(item){
	if(!item)
		return false;

	if(!item.meta["Magic Weapon"])
		return false;
	return true;
};

//通过主动法宝技能以及使用者找到法宝本体
MagicWeaponManager.findMagicWeaponBySkill = function(subject, skill){
	var magicWeapons = new Array();
	if(!DataManager.isSkill(skill))
		return magicWeapons;

	for(var i = 0; i < subject._equips.length; i++){
		var equip = subject._equips[i];
		if(!equip) //当装备者没有装备某部位则跳过
			continue;
		if(!equip.object())
			continue;

		var traits = equip.object().traits;

		for(var j = 0; j < traits.length; j++){
			if(traits[j].code === Game_BattlerBase.TRAIT_SKILL_ADD 
				&& traits[j].dataId === skill.id
				&& traits[j].value !== 0)
					magicWeapons.push(equip);
		}
	}

	return magicWeapons;
};

//根据装备原始id得到装备对象
MagicWeaponManager.findMagicWeaponByBaseItemId = function(subject, baseId, isWeapon){
	var equips = new Array();

	for(var i = 0; i < subject._equips.length; i++){
		var equip = subject._equips[i];
		if(!equip)
			continue;
		if(!equip.object())
			continue;
		if(equip.isWeapon() !== isWeapon)
			continue;

		if(equip.object().baseItemId && equip.object().baseItemId === baseId)
			equips.push(equip);
		else if(!equip.object().baseItemId && equip.object().id === baseId)
			equips.push(equip);
	}

	return equips;
};

//获取指定人员指定位置的法宝等级
MagicWeaponManager.getLevel = function(subject, slot){
	return subject._equips[slot].level();
};

//封装装备者id
MagicWeaponManager.INIT_ITEM = Game_Item.prototype.initialize;
Game_Item.prototype.initialize = function(item) {
    MagicWeaponManager.INIT_ITEM.call(this, item);

    this._equipActorId = -1;
};

Game_Item.prototype.setEquipActor = function(actor){
	this._equipActorId = actor.actorId();
};

Game_Item.prototype.equipActor = function(){
	return $gameActors.actor(this._equipActorId);
};

MagicWeaponManager.INIT_ACTOR_EQUIPS = Game_Actor.prototype.initEquips;
Game_Actor.prototype.initEquips = function(equips){
	MagicWeaponManager.INIT_ACTOR_EQUIPS.call(this, equips);

	this.initEquipActor();
};

Game_Actor.prototype.initEquipActor = function(){
	for(var i = 0; i < this._equips.length; i++){
		var equip = this._equips[i];
		if(equip)
			equip.setEquipActor(this);
	}
};

if(Imported.YEP_EquipCore){

MagicWeaponManager.ACTOR_EQUIPS = Game_Actor.prototype.equips;
Game_Actor.prototype.equips = function() {
	var equips = MagicWeaponManager.ACTOR_EQUIPS.call(this);

	this.initEquipActor();

	return equips;
};

MagicWeaponManager.CHANGE_ACTOR_EQUIP = Game_Actor.prototype.changeEquip;
Game_Actor.prototype.changeEquip = function(slotId, item) {
	MagicWeaponManager.CHANGE_ACTOR_EQUIP.call(this, slotId, item);

	this.initEquipActor();
};

MagicWeaponManager.FORCE_CHANGE_ACTOR_EQUIP = Game_Actor.prototype.forceChangeEquip;
Game_Actor.prototype.forceChangeEquip = function(slotId, item){
	MagicWeaponManager.FORCE_CHANGE_ACTOR_EQUIP.call(this, slotId, item);

	this.initEquipActor();
};

}

//是否为【法宝属性】装备(带有【等级】和【经验】的装备)
Game_Item.prototype.isMagicWeapon = function(){
	return MagicWeaponManager.isMagicWeapon(this.object()) && this.isEquipItem();
};

//法宝等级
Game_Item.prototype.level = function(){
	if(!this.isMagicWeapon())
		return -1;

	return this.object().level;
};

Game_Item.prototype.maxLevel = function(){
	if(!this.isMagicWeapon())
		return -1;

	return this.object().maxLevel;
};

//法宝当前经验
Game_Item.prototype.exp = function(){
	if(!this.isMagicWeapon())
		return -1;

	return this.object().exp;
};

//设置法宝当前经验
Game_Item.prototype.setExp = function(exp){
	this.object().exp = exp;
};

//法宝下一级经验
Game_Item.prototype.nextLevelExp = function(){
	var level = this.level();
	if(level < 0)
		return -1;

	var initFormla = "3 * level * level + 40 * level + 50";
	var formula = MagicWeaponManager.EXP_FORMULA;
	var item = this.object();
	if(!item.expFormula)
		item.expFormula = formula;
	var nextExp = 0;

	try{
		nextExp = eval(item.expFormula);
	}catch(err){
		nextExp = eval(initFormla);
		console.error(err);
	}

    return Math.round(nextExp);
};

//使用该法宝主动技时，法宝经验增加值
Game_Item.prototype.useExp = function(){
	if(!this.isMagicWeapon())
		return -1;
	return this.object().useExp;
};

//敌人法宝经验
Game_Enemy.prototype.magicWeaponExp = function(){
	return Math.round(this.enemy().magicWeaponExp);
};

//敌群法宝经验
Game_Troop.prototype.magicWeaponExpTotal = function(){
	var enemies = this.members();
	var exp = 0;

	for(i = 0; i < enemies.length; i++)
		exp += enemies[i].magicWeaponExp();

	return exp;
};

//获取法宝经验
Game_Item.prototype.gainMagicWeaponExp = function(inBattle){
	if(!this.isMagicWeapon())
		return;
	
	var getExp = $gameTroop.magicWeaponExpTotal();
	if(!inBattle)
		getExp = Math.round(getExp * MagicWeaponManager.EXP_RATE_NOT_BATTLER);

	this.gainExp(getExp);
};

Game_Item.prototype.gainExp = function(exp){
	if(!this.isMagicWeapon())
		return;

	if(this.level() >= this.maxLevel())
		return;

	var realExp = Math.round(this.exp() + exp);
	this.setExp(realExp);

	while(this.level() < this.maxLevel() && realExp >= this.nextLevelExp()){
		if(this.levelUp())
			realExp = this.exp();
		else
			break;
	}
};

//法宝升级
Game_Item.prototype.levelUp = function(){
	if(!this.isMagicWeapon())
		return false;

	var item = this.object();
	var exp = this.exp();
	var nextLevelExp = this.nextLevelExp();

	if(item.level < this.maxLevel()){
		item.level++;
		var nextExp = exp - nextLevelExp;
		if(nextExp < 0)
			nextExp = 0;
		this.setExp(nextExp);
		this.processLevelUpEffect();
	}else{
		this.setExp(nextLevelExp);
	}

	return true;
};

Game_Item.prototype.processLevelUpEffect = function(){
	var item = this.object();
	var user = this.equipActor();

	try{
		if(item.equipLevelUpEffect)
			eval(item.equipLevelUpEffect);
	}catch(err){
		console.error(err)
	}
};

//寻找指定法宝的主动技能，返回数组
Game_Item.prototype.findMagicWeaponSkillIds = function(){
	if(!MagicWeaponManager.isMagicWeapon(this.object()))
		return null;

	var traits = this.object().traits;
	if(!traits)
		return null;

	var arr = new Array();
	traits.forEach(function(trait){
		if(trait.code === Game_BattlerBase.TRAIT_SKILL_ADD
			&& value !== 0)
			arr.push(trait.dataId);
	});

	return arr;
};

//人物-法宝经验结算
Game_Actor.prototype.gainMagicWeaponExp = function(){
	var equips = this._equips;

	for(var i = 0; i < equips.length; i++){
		var item = equips[i];
		if(!item.isMagicWeapon())
			continue;

		item.gainMagicWeaponExp(this.isBattleMember());
	}
};

//战斗结算
MagicWeaponManager.GAIN_REWARDS = BattleManager.gainRewards;
BattleManager.gainRewards = function(){
	MagicWeaponManager.GAIN_REWARDS.call(this);

	$gameParty.allMembers().forEach(function(actor){
		actor.gainMagicWeaponExp();
	});
};

MagicWeaponManager.APPLY_ACTION = Game_Action.prototype.apply;
Game_Action.prototype.apply = function(target) {
   MagicWeaponManager.APPLY_ACTION.call(this, target);

   var item = this.item();
   var subject = this.subject();
  
   if(!subject.isActor()) return; //敌人行动不做任何处理
   
   var magicWeapons = MagicWeaponManager.findMagicWeaponBySkill(subject, item);
   if(magicWeapons.length === 0) return; //不是法宝技能不做任何处理

   //使用成功 增加法宝经验
   magicWeapons.forEach(function(magicWeapon){
   		magicWeapon.gainActionExp();
   });
};

Game_Item.prototype.gainActionExp = function(){
   this.gainExp(this.useExp());
};

//添加技能类型处理，取消重复
MagicWeaponManager.ADDED_SKILL_TYPES = Game_BattlerBase.prototype.addedSkillTypes;
Game_BattlerBase.prototype.addedSkillTypes = function(){
	var oldStypes = MagicWeaponManager.ADDED_SKILL_TYPES.call(this);
	var newStypes = new Array();

	oldStypes.forEach(function(stypeId){
		if(!newStypes.contains(stypeId))
			newStypes.push(stypeId);
	});

	return newStypes;
};

//法宝强制升级
Game_Actor.prototype.forceMagicWeaponLevelUp = function(slot){
	var item = this._equips[slot];
	if(!MagicWeaponManager.isMagicWeapon(item.object()))
		return;

	item.levelUp();
};

MagicWeaponManager.PLUGIN_COMMAND = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
    MagicWeaponManager.PLUGIN_COMMAND.call(this, command, args);

    if(command == 'MagicWeaponLevelUp'){
    	var actorId = Number(args[0]) || 0;
    	var slot = Number(args[1]) || 0;
    	var level = Number(args[2]) || 0;

    	var actor = $gameActors.actor(actorId);
    	if(actor){
    		for(var i = 0; i < level; i++)
    			actor.forceMagicWeaponLevelUp(slot);
    	}
    }

};

//法宝信息窗口，显示等级与经验
//Window_MagicWeaponData
if(MagicWeaponManager.USE_UI_CODE){

function Window_MagicWeaponData() {
    this.initialize.apply(this, arguments);
};

Window_MagicWeaponData.winHeight = function(){
	return Window_Base.prototype.fittingHeight(2);
};

Window_MagicWeaponData.prototype = Object.create(Window_Base.prototype);
Window_MagicWeaponData.prototype.constructor = Window_MagicWeaponData;

Window_MagicWeaponData.prototype.initialize = function(x, y, listWindow, standardWindow, actorWindow) {
	this.setListWindow(listWindow);
	this._standardWindow = standardWindow;
	this._actorWindow = actorWindow;

	var width = this.windowWidth();
    var height = this.windowHeight();
    Window_Base.prototype.initialize.call(this, x, y, width, height);

    this.clear();
    this.deactivate();
    this.refresh();
};

Window_MagicWeaponData.prototype.clear = function(){
	this._item = null;
};

Window_MagicWeaponData.prototype.setItem = function(item){
	this._item = item;
	this.refresh();
};

Window_MagicWeaponData.prototype.setListWindow = function(listWindow){
	this._listWindow = listWindow;
};

Window_MagicWeaponData.prototype.windowHeight = function() {
    return Window_MagicWeaponData.winHeight();
};

Window_MagicWeaponData.prototype.windowWidth = function(){
	var standardWindow = this._standardWindow;
	if(!standardWindow)
		standardWindow = this._listWindow;
	return standardWindow.width;
};

MagicWeaponManager.DRAW_BASE_ITEM_NAME = Window_Base.prototype.drawItemName;
Window_Base.prototype.drawItemName = function(item, x, y, width) {
    MagicWeaponManager.DRAW_BASE_ITEM_NAME.call(this, item, x, y, width);

    var itemObject = new Game_Item();
    itemObject.setObject(item);

    if(itemObject.isNull() || !itemObject.isMagicWeapon())
    	return;

    this.drawLevelOnIcon(x + 2, y + 2, itemObject.level());
};

Window_MagicWeaponData.prototype.drawItem = function(){
	if(!this._item || this._item.isNull() || !this._listWindow.active)
		return;

	this.drawItemIconAndLevel(0, 0);
	this.drawItemName(Window_Base._iconWidth + this.standardPadding() / 4, 0);
	if(this._item.isMagicWeapon()){
		this.drawItemLevel(this.windowWidth() - this.textWidth("00000000000000") - this.standardPadding() * 2, 0);
		this.drawItemExp(this.standardPadding(), this.lineHeight());
	}
};

Window_MagicWeaponData.prototype.drawItemIconAndLevel = function(x, y){
	var iconIndex = this._item.object().iconIndex;
	var level = this._item.level();

	this.drawIcon(iconIndex, x, y);
	if(level > 0)
		this.drawLevelOnIcon(x, y, level);
};

Window_Base.prototype.drawLevelOnIcon = function(x, y, level){
	this.contents.fontSize = MagicWeaponManager.LEVEL_FONT_SIZE;
	this.changePaintOpacity(true);
	this.changeTextColor(this.magiccWeaponLevelFontColor());
	this.drawText(level, x + Window_Base._iconWidth / 2 - 4, y - Window_Base._iconHeight / 4, this.textWidth("00"), 'right');
	this.resetTextColor();
	this.resetFontSettings();
}

Window_MagicWeaponData.prototype.drawItemName = function(x, y){
	var name = this._item.object().name;
	this.drawText(name, x, y);
	this.resetTextColor();
};

Window_MagicWeaponData.prototype.drawItemLevel = function(x, y){
	var level = this._item.level();
	var maxLevel = this._item.maxLevel();
	var rate = level / maxLevel;

	var width = Math.round(this.windowWidth() - x - this.standardPadding() / 2);

	var color1 = this.levelGaugeColor1();
	var color2 = this.levelGaugeColor2();

	this.drawGauge(x, y, width, rate, color1, color2);

	this.changeTextColor(this.systemColor());
	this.drawText("等级", x, y);
	this.resetTextColor();

	this.drawCurrentAndMax(level, maxLevel, x - this.standardPadding() * 3, y, width, this.normalColor(), this.normalColor());
};

Window_MagicWeaponData.prototype.drawItemExp = function(x, y){
	var exp = this._item.exp();
	var maxExp = this._item.nextLevelExp();
	var width = this.windowWidth() - this.standardPadding() / 2;
	var rate = exp / maxExp;
	if(this._item.level() === this._item.maxLevel())
		rate = 1;

	var color1 = this.expGaugeColor1();
	var color2 = this.expGaugeColor2();
	this.drawGauge(x, y, width, rate, color1, color2);

	this.changeTextColor(this.systemColor())
	this.drawText("经验", x, y);
	this.resetTextColor();

	if(this._item.level() === this._item.maxLevel()){
		this.drawText("MAX", x + 3 * width / 4 - this.textWidth("00"), y);
		this.resetTextColor();
	}
	else
		this.drawCurrentAndMax(exp, maxExp, x - this.standardPadding() * 3, y, width, this.normalColor(), this.normalColor());
};

Window_MagicWeaponData.prototype.refresh = function(){
	if(this.contents){
		this.contents.clear();
		this.drawItem();
	}
};

Window_MagicWeaponData.prototype.update = function(){
	Window_Base.prototype.update.call(this);

	if(this._actorWindow && this._actorWindow.active){
		this.hide();
	}
	else if(!this.visible)
		this.show();

	if(this._listWindow.active){
		if(MagicWeaponManager.selectFlag){
			var baseItem = this._listWindow.item();
			var item = new Game_Item();
			if(baseItem)
    			item.setObject(baseItem);
			if(this._lastItem !== item){
				this.setItem(item);
				this._lastItem = item;
			}
		}
	}

	this.refresh();

	MagicWeaponManager.selectFlag = false;
};

Window_Base.prototype.magiccWeaponLevelFontColor = function(){
	return this.textColor(MagicWeaponManager.LEVEL_FONT_COLOR);
};

Window_MagicWeaponData.prototype.levelGaugeColor1 = function(){
	return this.textColor(MagicWeaponManager.LEVEL_GAUGE_COLOR1);
};

Window_MagicWeaponData.prototype.levelGaugeColor2 = function(){
	return this.textColor(MagicWeaponManager.LEVEL_GAUGE_COLOR2);
};

Window_MagicWeaponData.prototype.expGaugeColor1 = function(){
	return this.textColor(MagicWeaponManager.EXP_GAUGE_COLOR1);
};

Window_MagicWeaponData.prototype.expGaugeColor2 = function(){
	return this.textColor(MagicWeaponManager.EXP_GAUGE_COLOR2);
};

MagicWeaponManager.SELECT_ITEM = Window_Selectable.prototype.select;
Window_Selectable.prototype.select = function(index) {
    MagicWeaponManager.SELECT_ITEM.call(this, index);
    if(this instanceof Window_EquipSlot || this instanceof Window_EquipItem
    	|| this instanceof Window_ItemList){
    	if(index >= 0)
    		MagicWeaponManager.selectFlag = true;//数据已经选择，准备更新
    }
};

MagicWeaponManager.ACTIVATE_SELECT = Window_Selectable.prototype.activate;
Window_Selectable.prototype.activate = function() {
    MagicWeaponManager.ACTIVATE_SELECT.call(this);

    var scene = SceneManager._scene;
    if(scene instanceof Scene_Equip){
    	if(this instanceof Window_EquipSlot || this instanceof Window_EquipItem){
    		scene._magicWeaponDataWindow.setListWindow(this);
    		var x = scene._magicWeaponDataWindow.x;
    		var y = scene._magicWeaponDataWindow.y;
    		var width = scene._magicWeaponDataWindow.windowWidth();
    		var height = scene._magicWeaponDataWindow.windowHeight();
    		scene._magicWeaponDataWindow.move(x, y, width, height);
    	}
    }

    if(scene instanceof Scene_Item){
    	if(this instanceof Window_ItemList){
    		scene._magicWeaponDataWindow.setListWindow(this);
    		var x = scene._magicWeaponDataWindow.x;
    		var y = scene._magicWeaponDataWindow.y;
    		var width = scene._magicWeaponDataWindow.windowWidth();
    		var height = scene._magicWeaponDataWindow.windowHeight();
    		scene._magicWeaponDataWindow.move(x, y, width, height);
    	}
    }
};

MagicWeaponManager.INIT_EQUIP_COMPARE = Window_StatCompare.prototype.initialize;
Window_StatCompare.prototype.initialize = function(wx, wy, ww, wh) {
    wh -= Window_MagicWeaponData.winHeight();
    MagicWeaponManager.INIT_EQUIP_COMPARE.call(this, wx, wy, ww, wh);
};

Window_StatCompare.prototype.drawParamName = function(y, paramId) {
    var x = this.textPadding() + this.standardPadding() * 2;
    this.changeTextColor(this.systemColor());
    this.drawText(TextManager.param(paramId), x, y, this._paramNameWidth);
};

MagicWeaponManager.INIT_ITEM_INFO = Window_ItemInfo.prototype.initialize;
Window_ItemInfo.prototype.initialize = function(x, y, width, height) {
    height -= Window_MagicWeaponData.winHeight();
    MagicWeaponManager.INIT_ITEM_INFO.call(this, x, y, width, height);
};

MagicWeaponManager.DRAW_COMPARE_ITEM = Window_StatCompare.prototype.drawItem;
Window_StatCompare.prototype.drawItem = function(x, y, paramId) {
	this.contents.fontSize -= 10;

    MagicWeaponManager.DRAW_COMPARE_ITEM.call(this, x, y - 8 * paramId, paramId);

    this.resetFontSettings();
};

Window_StatCompare.prototype.lineHeight = function(){
	var lineHeight = Window_Base.prototype.lineHeight.call(this);

	if(this.contents)
		lineHeight = this.contents.fontSize + 4;

	return lineHeight;
};

MagicWeaponManager.CREATE_SCENE_EQUIP = Scene_Equip.prototype.create;
Scene_Equip.prototype.create = function() {
    MagicWeaponManager.CREATE_SCENE_EQUIP.call(this);
    this.createMagicWeaponDataWindow(this._compareWindow.x, Graphics.boxHeight - Window_MagicWeaponData.winHeight());
};

Scene_Equip.prototype.createMagicWeaponDataWindow = function(x, y){
	this._magicWeaponDataWindow = new Window_MagicWeaponData(x, y, this._slotWindow, this._compareWindow);

	this.addWindow(this._magicWeaponDataWindow);
};

MagicWeaponManager.CREATE_SCENE_ITEM = Scene_Item.prototype.create;
Scene_Item.prototype.create = function() {
    MagicWeaponManager.CREATE_SCENE_ITEM.call(this);

    this.createMagicWeaponDataWindow(this._infoWindow.x, Graphics.boxHeight - Window_MagicWeaponData.winHeight());
};

Scene_Item.prototype.createMagicWeaponDataWindow = function(x, y){
	this._magicWeaponDataWindow = new Window_MagicWeaponData(x, y, this._itemWindow, this._infoWindow, this._actorWindow);

	this.addWindow(this._magicWeaponDataWindow);
};

}